package com.zhl.controller;

import com.zhl.entity.Function;
import com.zhl.entity.Role;
import com.zhl.entity.User;
import com.zhl.entity.ZhlResult;
import com.zhl.service_.RoleService;
import com.zhl.service_.UserService;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.LockedAccountException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2018/11/6.
 */
@Controller
public class RoleController {

    Logger logger = LoggerFactory.getLogger("RoleController");

    @Autowired
    private RoleService roleService;

    //查询所有用户
    @RequestMapping("/api/role")
    @ResponseBody
    public ZhlResult rolelist(HttpServletRequest request) {

        List<Role> users = roleService.rolelist();
        return ZhlResult.ok(users);

    }

    //根据id查询角色
    @RequestMapping("/api/setfunction/{id}")
    public String userlist(@PathVariable("id") String id, Model model) {

        Role roleByid = roleService.queryRoleByid(id);
        List<Function> functions = roleByid.getFunctions();
        String fids = "";
        for (Function function:functions) {
            String fid = function.getFid();
            fids += fid+",";
        }
        roleByid.setFids(fids);

        model.addAttribute("roleByid",roleByid);

        return "/admin/roledetail.html";

    }

    /**
     * 添加和修改权限
     * @param rid
     * @param fids
     * @return
     */
    @RequestMapping("/api/addrolefunction")
    @ResponseBody
    public ZhlResult addRoleFunction(String rid,String fids) {

        roleService.addRoleFunction(rid,fids);
        return ZhlResult.ok();

    }




}
